package com.example.mansystem.component;

import com.alibaba.fastjson.JSON;
import com.example.mansystem.entity.InstrumentDO;
import com.example.mansystem.mapper.InstrumentMapper;
import com.example.mansystem.service.DictService;
import com.example.mansystem.service.WebSocketServer;
import com.example.mansystem.vo.BatchErrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class AsyncTask {
    @Resource
    InstrumentMapper instrumentMapper;
    @Autowired
    DictService dictService;
    @Autowired
    WebSocketServer webSocketServer;
//    public String getExceptionMessage(Exception ex) {
//        StringWriter stringWriter= new StringWriter();
//        PrintWriter writer= new PrintWriter(stringWriter);
//        ex.printStackTrace(writer);
//        StringBuffer buffer= stringWriter.getBuffer();
//        return buffer.toString();
//    }
    private static List<Map<String,String>> mapList;
    @Autowired
    public void InitMap()
    {
        List<Map<String,String>> temp = dictService.getInstrumentCardDict();
        mapList=temp;
    }

    @Async
    public void AsyncInRegister(List<InstrumentDO> batch, AtomicInteger curCount, int size, String username,int start)
    {
        for(Integer i=0;i<batch.size();i++)
        {
            try{
                if(mapList.get(0).get(batch.get(i).getJFKM())==null)
                {
                    throw new Exception("经费科目字段出错，经费科目字典中无此code");
                }
                if(mapList.get(1).get(batch.get(i).getXZ())==null)
                {
                    throw new Exception("现状字段出错，现状字典中无此code");
                }
                if(mapList.get(2).get(batch.get(i).getSYFX())==null)
                {
                    throw new Exception("使用方向字段出错，使用方向字典中无此code");
                }
                if(mapList.get(3).get(batch.get(i).getXKLY())==null)
                {
                    throw new Exception("学科领域字段出错，现状字典中无此code");
                }
                if(mapList.get(4).get(batch.get(i).getGXMS())==null)
                {
                    throw new Exception("共享模式字段出错，共享模式字典中无此code");
                }

                instrumentMapper.insert((batch.get(i)));
            }catch (Exception e)
            {
                String err = e.getMessage();
                String res = JSON.toJSONString(BatchErrVO.builder().err(err).cur(start+i).build());
                webSocketServer.sendInfo(username,res);
            }

            if((i+1)%100==0)
            {
                int cur=curCount.addAndGet(100);
                webSocketServer.sendInfo(username,"{\"cur\":"+cur+",\"size\":"+size+"}");
            }
            else if(i==batch.size()-1)
            {
                int cur=curCount.addAndGet(batch.size()%100);
                webSocketServer.sendInfo(username,"{\"cur\":"+cur+",\"size\":"+size+"}");
            }
        }
    }
}
